import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BorrowDAO {
    // 添加借阅记录
    public boolean addBorrow(Borrow borrow) {
        String sql = "INSERT INTO borrows (book_id, reader_id, borrow_date, due_date, returned) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, borrow.getBookId());
            pstmt.setInt(2, borrow.getReaderId());
            pstmt.setDate(3, new Date(borrow.getBorrowDate().getTime()));
            pstmt.setDate(4, new Date(borrow.getDueDate().getTime()));
            pstmt.setBoolean(5, borrow.isReturned());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 查询所有借阅记录
    public List<Borrow> getAllBorrows() {
        List<Borrow> borrows = new ArrayList<>();
        String sql = "SELECT * FROM borrows";
        try (Connection conn = DatabaseConnection.getConnection();
             java.sql.Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Borrow borrow = new Borrow(rs.getInt("book_id"), rs.getInt("reader_id"), rs.getDate("borrow_date"), rs.getDate("due_date"));
                borrow.setId(rs.getInt("id"));
                borrow.setReturnDate(rs.getDate("return_date"));
                borrow.setReturned(rs.getBoolean("returned"));
                borrows.add(borrow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return borrows;
    }

    // 更新借阅记录
    public boolean updateBorrow(Borrow borrow) {
        String sql = "UPDATE borrows SET book_id = ?, reader_id = ?, borrow_date = ?, return_date = ?, due_date = ?, returned = ? WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, borrow.getBookId());
            pstmt.setInt(2, borrow.getReaderId());
            pstmt.setDate(3, new Date(borrow.getBorrowDate().getTime()));
            if (borrow.getReturnDate() != null) {
                pstmt.setDate(4, new Date(borrow.getReturnDate().getTime()));
            } else {
                pstmt.setDate(4, null);
            }
            pstmt.setDate(5, new Date(borrow.getDueDate().getTime()));
            pstmt.setBoolean(6, borrow.isReturned());
            pstmt.setInt(7, borrow.getId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 归还图书
    public boolean returnBook(int borrowId) {
        String sql = "UPDATE borrows SET return_date = ?, returned = ? WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setDate(1, new Date(new java.util.Date().getTime()));
            pstmt.setBoolean(2, true);
            pstmt.setInt(3, borrowId);
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取读者未归还的借阅记录
    public List<Borrow> getUnreturnedBorrowsByReader(int readerId) {
        List<Borrow> borrows = new ArrayList<>();
        String sql = "SELECT * FROM borrows WHERE reader_id = ? AND returned = false";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, readerId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Borrow borrow = new Borrow(rs.getInt("book_id"), rs.getInt("reader_id"), rs.getDate("borrow_date"), rs.getDate("due_date"));
                    borrow.setId(rs.getInt("id"));
                    borrow.setReturnDate(rs.getDate("return_date"));
                    borrow.setReturned(rs.getBoolean("returned"));
                    borrows.add(borrow);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return borrows;
    }

    // 获取逾期借阅记录
    public List<Borrow> getOverdueBorrows() {
        List<Borrow> borrows = new ArrayList<>();
        String sql = "SELECT * FROM borrows WHERE due_date < CURDATE() AND returned = false";
        try (Connection conn = DatabaseConnection.getConnection();
             java.sql.Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Borrow borrow = new Borrow(rs.getInt("book_id"), rs.getInt("reader_id"), rs.getDate("borrow_date"), rs.getDate("due_date"));
                borrow.setId(rs.getInt("id"));
                borrow.setReturnDate(rs.getDate("return_date"));
                borrow.setReturned(rs.getBoolean("returned"));
                borrows.add(borrow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return borrows;
    }
}